package com.cskaoyan.realm;

import com.cskaoyan.bean.pojo.admin.*;
import com.cskaoyan.dao.AdminMapper;
import com.cskaoyan.dao.AdminPermissionMapper;
import com.cskaoyan.dao.AdminRoleMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: lds
 * @clsss: CustomRealm
 */
@Configuration
public class AdminRealm extends AuthorizingRealm {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    AdminRoleMapper adminRoleMapper;
    @Autowired
    AdminPermissionMapper adminPermissionMapper;
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        MallToken token = (MallToken) authenticationToken;
        String username = token.getUsername();
        char[] password = token.getPassword();
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
//        System.out.println(username+new String(password));
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(new String(password));
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        String credential = admins.size()>=1?admins.get(0).getPassword():null;
//        System.out.println(credential);
//        System.out.println(admins.get(0));
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(admins.get(0), credential, this.getName());

        return authenticationInfo;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Admin primaryPrincipal = (Admin) principalCollection.getPrimaryPrincipal();
        String username = primaryPrincipal.getUsername();
        String password = primaryPrincipal.getPassword();
        Integer[] roleIds = primaryPrincipal.getRoleIds();

        AdminRoleExample adminRoleExample = new AdminRoleExample();
        AdminRoleExample.Criteria criteriaAdminRole = adminRoleExample.createCriteria();
        List<Integer> integerList = Arrays.asList(roleIds);
        criteriaAdminRole.andIdIn(integerList);
        List<AdminRole> adminRoles = adminRoleMapper.selectByExample(adminRoleExample);


        ArrayList<Integer> roles = new ArrayList<>();
        ArrayList<String> roleNames = new ArrayList<>();

        for (AdminRole adminRole : adminRoles) {
            roles.add(adminRole.getId());
            roleNames.add(adminRole.getName());
        }

        AdminPermissionExample adminPermissionExample = new AdminPermissionExample();
        AdminPermissionExample.Criteria criteria = adminPermissionExample.createCriteria();
        criteria.andRoleIdIn(roles);

        List<AdminPermission> adminPermissions = adminPermissionMapper.selectByExample(adminPermissionExample);
        ArrayList<String> perms = new ArrayList<>();
        for (AdminPermission adminPermission : adminPermissions) {
            perms.add(adminPermission.getPermission());
        }
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(perms);
        simpleAuthorizationInfo.addRoles(roleNames);
        return simpleAuthorizationInfo;
    }


}
